import $http from '../utils/http.js'
const Gettable = '/systemNode/Gettable' // 获取数据
const Muachangepage = '/systemNode/Muachangepage' // 主页面分页前提交MUA
const Muachangetable = '/systemNode/Muachangetable' // 获取数据Mua
const MuachangePageSize = '/systemNode/MuachangePageSize' // 改变每页条数
const Muasearch = '/systemNode/Muasearch' // 搜索前提交MUA
const MuaSort = '/systemNode/MuaSort' // 排序前提交MUA
const MuaNewnum = '/systemNode/MuaNewnum' // 获取同步数量提交到MUA
const returndata = '/systemNode/returndata' // 获取详情数据
const MuaReturndata = '/systemNode/MuaReturndata' // 获取详情数据提交到MUA
const getinitadd = '/systemNode/getinitadd' // 获取同步新增数据
const sortadd = '/systemNode/sortadd' // 同步新增排序
const getinitedit = '/systemNode/getinitedit' // 获取同步更改数据
const sortedit = '/systemNode/sortedit' // 同步更改排序
const getinitdel = '/systemNode/getinitdel'// 获取同步删除数据
const sortdel = '/systemNode/sortdel' // 同步删除排序
const gethisadd = '/systemNode/gethisadd' // 历史新增数据
const Muahischangepageadd = '/systemNode/Muahischangepageadd' // 历史新增分页前提交MUA
const MuahischangePageSizeadd = '/systemNode/MuahischangePageSizeadd' // 历史新增改变每页条数前提交Mua
const Muahissearchadd = '/systemNode/Muahissearchadd' // 历史新增搜索前提交MUA
const MuahisSortadd = '/systemNode/MuahisSortadd' // 历史新增排序前提交MUA
const gethisupdate = '/systemNode/gethisupdate' // 历史更改数据
const Muahischangepageedit = '/systemNode/Muahischangepageedit' // 历史更改分页前提交MUA
const MuahischangePageSizeedit = '/systemNode/MuahischangePageSizeedit' // 历史更改改变每页条数前提交Mua
const Muahissearchedit = '/systemNode/Muahissearchedit' // 历史更改搜索前提交MUA
const MuahisSortedit = '/systemNode/MuahisSortedit' // 历史更改排序前提交MUA
const gethisdelete = '/systemNode/gethisdelete' // 历史删除数据
const Muahischangepagedel = '/systemNode/Muahischangepagedel' // 历史删除分页前提交MUA
const MuahischangePageSizedel = '/systemNode/MuahischangePageSizedel' // 历史删除改变每页条数前提交Mua
const Muahissearchdel = '/systemNode/Muahissearchdel' // 历史删除搜索前提交MUA
const MuahisSortdel = '/systemNode/MuahisSortdel' // 历史删除排序前提交MUA
const deleteall = '/systemNode/deleteall' // 清空同步信息
const changeLoading = '/systemNode/changeLoading'
export default {
  state: {
    fullscreenLoading: true,
    table: [],
    msg: [{}],
    num: [{}],
    total: 10,
    trackState: [],
    tableBox: {
      tableData: {
        'pagination': {
          'current': 1, // 当前页码
          'pageSize': 20 // 当前页显示的数据条数
        },
        'search': {
          'value': '' // 需要模糊搜索的值（搜索框）
        },
        'sorter': {
          'field': 'ar_time', // 排序的字段
          'order': 'descend' // or "desc"升序还是降序
        },
        'filters': {
          // 需要被过滤的字段
          'aconfi_lv_id': []
        }
      },
      table: [], /* 图表数据 */
      total: 0 /* Y条数 */
    },
    history_add: {
      tableData: {
        'pagination': {
          'current': 1, // 当前页码
          'pageSize': 20 // 当前页显示的数据条数
        },
        'search': {
          'value': '' // 需要模糊搜索的值（搜索框）
        },
        'sorter': {
          'field': 'er_time', // 排序的字段
          'order': 'descend' // or "desc"升序还是降序
        }
      },
      table: [], /* 图表数据 */
      total: 0 /* Y条数 */
    },
    history_edit: {
      tableData: {
        'pagination': {
          'current': 1, // 当前页码
          'pageSize': 20// 当前页显示的数据条数
        },
        'search': {
          'value': '' // 需要模糊搜索的值（搜索框）
        },
        'sorter': {
          'field': 'er_time', // 排序的字段
          'order': 'descend' // or "desc"升序还是降序
        }
      },
      table: [], /* 图表数据 */
      total: 0 /* Y条数 */
    },
    history_del: {
      tableData: {
        'pagination': {
          'current': 1, // 当前页码
          'pageSize': 20// 当前页显示的数据条数
        },
        'search': {
          'value': '' // 需要模糊搜索的值（搜索框）
        },
        'sorter': {
          'field': 'er_time', // 排序的字段
          'order': 'descend' // or "desc"升序还是降序
        }
      },
      table: [], /* 图表数据 */
      total: 0 /* Y条数 */
    },
    syn_add: {
      tableData: {
        'sorter': {
          'field': 'er_time', // 排序的字段
          'order': 'descend' // or "desc"升序还是降序
        }
      },
      table: [], /* 图表数据 */
      total: 0 /* Y条数 */
    },
    syn_edit: {
      tableData: {
        'sorter': {
          'field': 'er_time', // 排序的字段
          'order': 'descend' // or "desc"升序还是降序
        }
      },
      table: [], /* 图表数据 */
      total: 0 /* Y条数 */
    },
    syn_del: {
      tableData: {
        'sorter': {
          'field': 'er_time', // 排序的字段
          'order': 'descend' // or "desc"升序还是降序
        }
      },
      table: [], /* 图表数据 */
      total: 0 /* Y条数 */
    }
  },
  actions: {
    // 获取表格数据,变更数量
    [Gettable] ({ commit, state, dispatch }, obj) {
      if (obj === undefined) {
        obj = 'type'
      }
      state.fullscreenLoading = true
      if (obj.type === 'pagCur') {
        commit(Muachangepage, obj.page)
      } else if (obj.type === 'pagSize') {
        commit(MuachangePageSize, obj.pageSizes)
      } else if (obj.type === 'search') {
        commit(Muasearch, { value: obj.value, currentPage: obj.currentPage })
      } else if (obj.type === 'sorter') {
        commit(MuaSort, { key: obj.key, value: obj.value })
      }
      const sat = {
        pagination: {
          'current': state.tableBox.tableData.pagination.current,
          'pageSize': state.tableBox.tableData.pagination.pageSize
        },
        search: {
          'value': state.tableBox.tableData.search.value
        },
        sorter: {
          'order': state.tableBox.tableData.sorter.order,
          'field': state.tableBox.tableData.sorter.field
        }
      }
      let p = []
      // 主页面数据获取
      p[1] = $http.option('/api/systemNode/list', sat
      ).then(function ({ data, status } = {}) {
        if (status === 'success') {
          commit(Muachangetable, data)
        }
      }).catch(function (err) {
        console.log(err)
      })
      // 变更详情数量获取
      p[2] = $http.get('/api/systemNode/new'
      ).then(function ({ data, status } = {}) {
        if (status === 'success') {
          commit(MuaNewnum, data)
        }
      }).catch(function (err) {
        console.log(err)
      })
      Promise.all(p).then(results => {
        state.fullscreenLoading = false
      })
    },
    // 获取表格详情
    [returndata] ({ state, commit }, sys_node_id) {
      $http.get('/api/systemNode/details', sys_node_id
      ).then(function ({ data, status } = {}) {
        if (status === 'success') {
          commit(MuaReturndata, data)
        }
      }).catch(function (err) {
        console.log(err)
      })
    },
    // 获取同步新增数据
    [getinitadd] ({ commit, state, dispatch }, obj) {
      if (obj === undefined) {
        obj = 'type'
      }
      if (obj.type === 'sorter') {
        commit(sortadd, { key: obj.key, value: obj.value })
      }
      const sat = {
        sorter: {
          'order': state.syn_add.tableData.sorter.order,
          'field': state.syn_add.tableData.sorter.field
        }
      }
      $http.option('/api/sync/systemNode/new_list_add', sat
      ).then(function ({ data, status } = {}) {
        if (status === 'success') {
          commit(getinitadd, data)
          commit(changeLoading)
        }
      }).catch(function (err) {
        console.log(err)
      })
    },
    // 获取同步更改数据
    [getinitedit] ({ commit, state, dispatch }, obj) {
      if (obj === undefined) {
        obj = 'type'
      }
      if (obj.type === 'sorter') {
        commit(sortedit, { key: obj.key, value: obj.value })
      }
      const sat = {
        sorter: {
          'order': state.syn_edit.tableData.sorter.order,
          'field': state.syn_edit.tableData.sorter.field
        }
      }
      commit(changeLoading)
      $http.option('/api/sync/systemNode/new_list_update', sat
      ).then(function ({ data, status } = {}) {
        if (status === 'success') {
          commit(getinitedit, data)
          commit(changeLoading)
        }
      }).catch(function (err) {
        console.log(err)
      })
    },
    // 获取同步删除数据
    [getinitdel] ({ commit, state, dispatch }, obj) {
      if (obj === undefined) {
        obj = 'type'
      }
      if (obj.type === 'sorter') {
        commit(sortdel, { key: obj.key, value: obj.value })
      }
      const sat = {
        sorter: {
          'order': state.syn_del.tableData.sorter.order,
          'field': state.syn_del.tableData.sorter.field
        }
      }
      commit(changeLoading)
      $http.option('/api/sync/systemNode/new_list_delete', sat
      ).then(function ({ data, status } = {}) {
        if (status === 'success') {
          commit(getinitdel, data)
          commit(changeLoading)
        }
      }).catch(function (err) {
        console.log(err)
      })
    },
    // 获取历史新增数据
    [gethisadd] ({ commit, state, dispatch }, obj) {
      if (obj === undefined) {
        obj = 'type'
      }
      if (obj.type === 'pagCur') {
        commit(Muahischangepageadd, obj.page)
      } else if (obj.type === 'pagSize') {
        commit(MuahischangePageSizeadd, obj.pageSizes)
      } else if (obj.type === 'search') {
        commit(Muahissearchadd, { value: obj.value, currentPage: obj.currentPage })
      } else if (obj.type === 'sorter') {
        commit(MuahisSortadd, { key: obj.key, value: obj.value })
      }
      const sat = {
        pagination: {
          'current': state.history_add.tableData.pagination.current,
          'pageSize': state.history_add.tableData.pagination.pageSize
        },
        search: {
          'value': state.history_add.tableData.search.value
        },
        sorter: {
          'order': state.history_add.tableData.sorter.order,
          'field': state.history_add.tableData.sorter.field
        }
      }
      $http.option('/api/sync/systemNode/list_add', sat
      ).then(function ({ data, status } = {}) {
        if (status === 'success') {
          commit(gethisadd, data)
          commit(changeLoading)
        }
      }).catch(function (err) {
        console.log(err)
      })
    },
    // 获取历史更改数据
    [gethisupdate] ({ commit, state, dispatch }, obj) {
      if (obj === undefined) {
        obj = 'type'
      }
      if (obj.type === 'pagCur') {
        commit(Muahischangepageedit, obj.page)
      } else if (obj.type === 'pagSize') {
        commit(MuahischangePageSizeedit, obj.pageSizes)
      } else if (obj.type === 'search') {
        commit(Muahissearchedit, { value: obj.value, currentPage: obj.currentPage })
      } else if (obj.type === 'sorter') {
        commit(MuahisSortedit, { key: obj.key, value: obj.value })
      }
      const sat = {
        pagination: {
          'current': state.history_edit.tableData.pagination.current,
          'pageSize': state.history_edit.tableData.pagination.pageSize
        },
        search: {
          'value': state.history_edit.tableData.search.value
        },
        sorter: {
          'order': state.history_edit.tableData.sorter.order,
          'field': state.history_edit.tableData.sorter.field
        }
      }
      $http.option('/api/sync/systemNode/list_update', sat
      ).then(function ({ data, status } = {}) {
        if (status === 'success') {
          commit(gethisupdate, data)
          commit(changeLoading)
        }
      }).catch(function (err) {
        console.log(err)
      })
    },
    // 获取历史删除数据
    [gethisdelete] ({ commit, state, dispatch }, obj) {
      if (obj === undefined) {
        obj = 'type'
      }
      if (obj.type === 'pagCur') {
        commit(Muahischangepagedel, obj.page)
      } else if (obj.type === 'pagSize') {
        commit(MuahischangePageSizedel, obj.pageSizes)
      } else if (obj.type === 'search') {
        commit(Muahissearchdel, { value: obj.value, currentPage: obj.currentPage })
      } else if (obj.type === 'sorter') {
        commit(MuahisSortdel, { key: obj.key, value: obj.value })
      }
      const sat = {
        pagination: {
          'current': state.history_del.tableData.pagination.current,
          'pageSize': state.history_del.tableData.pagination.pageSize
        },
        search: {
          'value': state.history_del.tableData.search.value
        },
        sorter: {
          'order': state.history_del.tableData.sorter.order,
          'field': state.history_del.tableData.sorter.field
        }
      }
      $http.option('/api/sync/systemNode/list_delete', sat
      ).then(function ({ data, status } = {}) {
        if (status === 'success') {
          commit(gethisdelete, data)
          commit(changeLoading)
        }
      }).catch(function (err) {
        console.log(err)
      })
    },
    // 删除同步信息
    [deleteall] ({ state, dispatch, commit }) {
      $http.post('/api/sync/systemNode/delete_list'
      ).then(function ({ data, status } = {}) {
        if (status === 'success') {
          dispatch(getinitadd)
          dispatch(getinitedit)
          dispatch(getinitdel)
        }
      }).catch(function (err) {
        console.log(err)
      })
    }
  },
  mutations: {
    // 主页面设置表格数据
    [Muachangetable] (state, data) {
      state.tableBox.table = data.dataSource
      state.tableBox.total = data.total
      let t = ['已注册', '未注册']
      for (let i in state.tableBox.table) {
        state.tableBox.table[i]['ais_register'] = t[parseFloat(state.tableBox.table[i]['ais_register']) - 1]
      }
    },
    // 主页面分页前提交MUA
    [Muachangepage] (state, page) {
      state.tableBox.tableData.pagination.current = page
    },
    // 主页面改变每页条数前提交到MUA
    [MuachangePageSize] (state, pageSizes) {
      state.tableBox.tableData.pagination.pageSize = pageSizes
    },
    // 主页面搜索前提交到MUA
    [Muasearch] (state, query) {
      state.tableBox.tableData.search.value = query.value
      state.tableBox.tableData.pagination.current = query.currentPage
    },
    // 表格排序前提交MUA
    [MuaSort] (state, { key, value }) {
      state.tableBox.tableData.sorter.order = value
      state.tableBox.tableData.sorter.field = key
    },
    // 获取同步数量提交到MUA
    [MuaNewnum] (state, data) {
      state.num = data
    },
    // 获取详情数据提交到MUA
    [MuaReturndata] (state, data) {
      state.msg = data[0]
      let t = ['已注册', '未注册']
      state.msg['ais_register'] = t[parseFloat(state.msg['ais_register']) - 1]
      for (var i in state.msg) {
        if (state.msg[i] === null || state.msg[i] === 'None') {
          state.msg[i] = '-'
        }
      }
    },
    // 同步新增数据
    [getinitadd] (state, data) {
      state.syn_add.table = data.dataSource
      state.syn_add.total = data.total
    },
    // 同步新增排序
    [sortadd] (state, { key, value }) {
      state.syn_add.tableData.sorter.order = value
      state.syn_add.tableData.sorter.field = key
    },
    // 同步更改数据
    [getinitedit] (state, data) {
      state.syn_edit.table = data.dataSource
      state.syn_edit.total = data.total
    },
    // 同步更改排序
    [sortedit] (state, { key, value }) {
      state.syn_edit.tableData.sorter.order = value
      state.syn_edit.tableData.sorter.field = key
    },
    // 同步删除数据
    [getinitdel] (state, data) {
      state.syn_del.table = data.dataSource
      state.syn_del.total = data.total
    },
    // 同步删除排序
    [sortdel] (state, { key, value }) {
      state.syn_del.tableData.sorter.order = value
      state.syn_del.tableData.sorter.field = key
    },
    // 历史新增分页前提交MUA
    [Muahischangepageadd] (state, page) {
      state.history_add.tableData.pagination.current = page
    },
    // 历史新增改变每页条数前提交到MUA
    [MuahischangePageSizeadd] (state, pageSizes) {
      state.history_add.tableData.pagination.pageSize = pageSizes
    },
    // 历史新增搜索前提交到MUA
    [Muahissearchadd] (state, query) {
      state.history_add.tableData.search.value = query.value
      state.history_add.tableData.pagination.current = query.currentPage
    },
    // 历史新增表格排序前提交MUA
    [MuahisSortadd] (state, { key, value }) {
      state.history_add.tableData.sorter.order = value
      state.history_add.tableData.sorter.field = key
    },
    // 历史新增数据
    [gethisadd] (state, data) {
      state.history_add.table = data.dataSource
      state.history_add.total = data.total
    },
    // 历史更改数据
    [gethisupdate] (state, data) {
      state.history_edit.table = data.dataSource
      state.history_edit.total = data.total
    },
    // 历史更改分页前提交MUA
    [Muahischangepageedit] (state, page) {
      state.history_edit.tableData.pagination.current = page
    },
    // 历史更改改变每页条数前提交到MUA
    [MuahischangePageSizeedit] (state, pageSizes) {
      state.history_edit.tableData.pagination.pageSize = pageSizes
    },
    // 历史更改搜索前提交到MUA
    [Muahissearchedit] (state, query) {
      state.history_edit.tableData.search.value = query.value
      state.history_edit.tableData.pagination.current = query.currentPage
    },
    // 历史更改表格排序前提交MUA
    [MuahisSortedit] (state, { key, value }) {
      state.history_edit.tableData.sorter.order = value
      state.history_edit.tableData.sorter.field = key
    },
    // 历史删除数据
    [gethisdelete] (state, data) {
      state.history_del.table = data.dataSource
      state.history_del.total = data.total
    },
    // 历史删除分页前提交MUA
    [Muahischangepagedel] (state, page) {
      state.history_del.tableData.pagination.current = page
    },
    // 历史删除改变每页条数前提交到MUA
    [MuahischangePageSizedel] (state, pageSizes) {
      state.history_del.tableData.pagination.pageSize = pageSizes
    },
    // 历史删除搜索前提交到MUA
    [Muahissearchdel] (state, query) {
      state.history_del.tableData.search.value = query.value
      state.history_del.tableData.pagination.current = query.currentPage
    },
    // 历史删除表格排序前提交MUA
    [MuahisSortdel] (state, { key, value }) {
      state.history_del.tableData.sorter.order = value
      state.history_del.tableData.sorter.field = key
    },
    // 改变loading
    [changeLoading] (state) {
      state.fullscreenLoading = !state.fullscreenLoading
    }
  }
}
